#include <iostream>
#include "gameActions.h"
using namespace std;


/*----------------------------------------------------------------------------*/
//Constructor
gameActions::gameActions()
{
  field = NULL;
}
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
//Constructor -- Use this one!
gameActions::gameActions(int** array, int cols, int rows)
{
  field = new int*[rows];
  for (int i = 0; i < rows; i++)
      field[i] = new int[cols];

  for (int r = 0; r < rows; r++)
      for (int c = 0; c < cols; c++)
           field[r][c] = array[r][c];
  col = cols;
  row = rows;
}
/*----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
/*** Check for a winner in a given row and return EMPTY(0?) if there is no
     winner, or the color of winning side RED(1?) or BLUE(2?)***/


int gameActions::checkWinnerRow(int rows)
{
//Needs tweaking for the actual game board once we get the code for that
    for (int i = 0; i < col-1; i++)
    {
        if ( (field[rows][i] != 0) &&
           (field[rows][i] == field[rows][i+1]) &&
           (field[rows][i] == field[rows][i+2]) &&
           (field[rows][i] == field[rows][i+3]))
           {
                          return field[rows][i];
           }

    }

        // return EMPTY if no winner
        return 0;
};
/*----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
/*** Check for a winner in a given column and return EMPTY(0?) if there is no
     winner, or the color of winning side RED(1?) or BLUE(2?)***/


int gameActions::checkWinnerCol(int cols)
{
//Needs tweaking for the actual game board once we get the code for that
    for (int i = 0; i < row - 3; i++)
    {
        if ((field[i][cols] != 0) &&
           (field[i][cols] == field[i+1][cols]) &&
           (field[i][cols] == field[i+2][cols]) &&
           (field[i][cols] == field[i+3][cols]))
           {
                          return field[i][cols];
           }

    }

        // return EMPTY if no winner
        return 0;
}
/*----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
/*** Check for a winner in a given diagonal and return EMPTY(0?) if there is no
     winner, or the color of winning side RED(1?) or BLUE(2?)***/

//Needs tweaking when we get the game board code
int gameActions::checkWinnerDiag(int rows, int cols)
{
	for (int j = 0; j < (col - 3); j++)
	    for (int i = 0; i < (row - 3); i++)
    {
        if ((field[i][j] != 0) &&
           (field[i][j] == field[i+1][j+1]) &&
           (field[i][j] == field[i+2][j+2]) &&
           (field[i][j] == field[i+3][j+3]))
           {
                          return field[i][j];
           }

    }
	
	for (int n = 0; n < col; n++)
	    for (int m = 3; m < row; m++)
    {
        if ((field[m][n] != 0) &&
           (field[m][n] == field[m-1][n+1]) &&
           (field[m][n] == field[m-2][n+2]) &&
           (field[m][n] == field[m-3][n+3]))
           {
                          return field[m][n];
           }

    }
	
	
	   //int x = rows, y = cols;

       // // move to top-left
       // while ((x > 0) && (y > 0)) {
       //     x--;
       //     y--;
       // }

       // // check down-right arrows
       // while ((x < (row - 3)) && (y < (col - 3)))
       // {

       //     if (field[x][y] != 0)
       //         if ((x+1) < row && (y+1) < col && field[x][y] == field[x+1][y+1])
       //            if ((x+2) < row && (y+2) < col && field[x+1][y+1] == field[x+2][y+2])
       //               if((x+3) < row && (y+3) < col && field[x+2][y+2] == field[x+3][y+3])
       //                  {
       //                         return field[x][y];
       //                  }

       //     x++;
       //     y++;
       // }
       // 
       // // reset for next test
       // x = rows;
       // y = cols;

       // // move to top-right
       // while ((x < (row - 1)) && (y > 0)) {
       //     x++;
       //     y--;
       // }

       // // check down-left arrows
       //while ((x > 0) && (y < (col - 3)))
       //{
       //      if (field[x][y] != 0)
       //         if ((x-1) < row && (y+1) < col && field[x][y] == field[x-1][y+1])
       //            if ((x-2) < row && (y+2) < col && field[x-1][y+1] == field[x-2][y+2])
       //               if((x-3) < row && (y+3) < col && field[x-2][y+2] == field[x-3][y+3])
       //                  {
       //                         return field[x][y];
       //                  }
       //     x--;
       //     y++;
       // }

        // return EMPTY if there is no winner
        return 0;
    }
/*----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
/*** Check for a tie in and return true for a tie or false to keep playing.***/
bool gameActions::checkTie()
{
  int check = 0;
  for (int i = 0; i < col; i++)
      if (field[0][i] != 0) //Needs to be board[firstGameRow][i] instead, see why below
         check++;

  if (check == col)
     return true;
  /*This needs to be something like
  if (check == numGameCols) instead,
  but we need the field's format before we can do that */

  else
      return false;

}
/*----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
void gameActions::dropChecker(int player, int& moveX, int& moveY)
{
        int rows = row -1;
        int dropCol;

        cout << "Player " << player << " please enter the column number you";
        cout << " wish to drop your token into: ";
        cin >> dropCol;
        dropCol--;

        while(dropCol < 0 || dropCol >= col || field[0][dropCol] != 0 )
        {
		cout << endl << "\aInvalid Entry - Please try again: ";
		cin >> dropCol;
                dropCol--;
        }

        for (int i = rows; i >= 0; i--)
        {
            if (field[i][dropCol] == 0)
            {
                // we can put our piece here...
                field[i][dropCol] = player;
                moveX = dropCol;
                moveY = i;
                // stop the loop
                i = -1;
            }
        }
}
/*----------------------------------------------------------------------------*/
/*this function just switches the player - call it at the end of a turn to change it*/
void gameActions::switchPlayer(int& player){
	if(player == 1)
		player = 2;
	else
		player = 1;
}


/*----------------------------------------------------------------------------*/
gameActions::~gameActions()
{
        for (int i = 0; i < row; i++)
		delete [] field[i];
	delete [] field;
}
/*----------------------------------------------------------------------------*/

/*
/*This function gets the column number the user wishes to drop the token into in addition to making sure the response is valid.
if an invalid response is given, the user must re-enter until a valid anser is given
int gameActions::getDropCol(int rows, int cols/*, int** boardnot sure if that's how you pass it){
	int dropCol;

	//data will need to be formatted once we get this thing running
	cout << "Enter the column number your wish to drop your token into: ";
	cin >> dropCol;

	//This while loop check for 3 things: 1) that the column number is not less than 1; 2)that the column number is not greater than the maximum number of columns given;  3)that the column number given is not already full of tokens
	while(dropCol < 1 || dropCol > cols /*|| field[rows][dropCol] != 0*/ /*This assumes the array will be made of numbers and zero is a neutral space - must be edited otherwise ){
		cout << endl << "\aInvalid Entry - Please enter a valid column number: ";
		cin >> dropCol;
		}//end of while loop that validates data entry
	return dropCol;

}
/*----------------------------------------------------------------------------*/
/*This function inserts the player number into the correct part of the array so that his or her token will be saved there.
void gameActions::dropToken(int dropcolumn in which token is dropped, int player player number - used to change the array , int** []boardagain - I need to make sure this is right){
	int i = row;

	//this loop checks to see if a token is beneath the current spot; it walks down the board until it finds another token or the bottom.
	while(field[i][drop] != 0 && i !=0){
		i--;
	}

	field[i][drop] = player;//sets the found position to the player's number
}
*/
